package com.huarui.purchaseOrder.service;

import java.util.HashMap;
import java.util.Map;
import org.ewaf.framework.service.BaseService;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.service.provider.task.PurOrSaleSaveTaskProviderImpl;
import com.huarui.purchaseOrder.service.provider.task.PurOrSaleUpdateTaskProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskDetail.PurOrSaleSaveTaskDetailProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskOwner.PurOrSaleDeleteTaskOwnerProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskOwner.PurOrSaleSaveTaskOwnerProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskOwner.PurOrSaleSubmitTaskOwnerProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskOwner.PurOrSaleTakeOverTaskOwnerProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskOwner.PurOrSaleTakeOverToUpdateTaskOwnerProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskOwner.PurOrSaleUpdateTaskOwnerProviderImpl;
import com.huarui.purchaseOrder.service.provider.taskOwner.PurOrSaleUpdateToSubmitTaskOwnerProviderImpl;
import com.huarui.task.dao.TaskDao;
import com.huarui.task.dao.TaskDetailDao;
import com.huarui.task.dao.TaskOwnerDao;
import com.huarui.task.model.Task;
import com.huarui.task.model.TaskDetail;
import com.huarui.task.model.TaskOwner;
import com.huarui.task.service.TaskEngineService;
import com.huarui.task.service.provider.TaskDetailProvider;
import com.huarui.task.service.provider.TaskOwnerProvider;
import com.huarui.task.service.provider.TaskProvider;

public class PurOrSaleTaskService extends BaseService{
	private TaskDao taskDao;
	private TaskDetailDao taskDetailDao;
	private TaskOwnerDao taskOwnerDao;
	private TaskEngineService taskEngineService;
	
	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	public void setTaskDetailDao(TaskDetailDao taskDetailDao) {
		this.taskDetailDao = taskDetailDao;
	}

	public void setTaskOwnerDao(TaskOwnerDao taskOwnerDao) {
		this.taskOwnerDao = taskOwnerDao;
	}

	public void setTaskEngineService(TaskEngineService taskEngineService) {
		this.taskEngineService = taskEngineService;
	}

	public boolean saveOrders(PurOrSaleOrder orders) {
		TaskProvider taskProvider = new PurOrSaleSaveTaskProviderImpl(orders);
		TaskDetailProvider taskDetailProvider = new PurOrSaleSaveTaskDetailProviderImpl(orders, null);
		TaskOwnerProvider taskOwnerProvider = new PurOrSaleSaveTaskOwnerProviderImpl(orders);
		
		return taskEngineService.create(taskProvider, taskDetailProvider, taskOwnerProvider);
	}
	
	public boolean submitOrders(PurOrSaleOrder orders) {
		TaskProvider taskProvider = new PurOrSaleSaveTaskProviderImpl(orders);
		TaskDetailProvider taskDetailProvider = new PurOrSaleSaveTaskDetailProviderImpl(orders, null);
		TaskOwnerProvider taskOwnerProvider = new PurOrSaleSubmitTaskOwnerProviderImpl(orders);
		
		return taskEngineService.create(taskProvider, taskDetailProvider, taskOwnerProvider);
	}
	
	private boolean update(PurOrSaleOrder orders, Integer ownerId) {
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("workFlowId", orders.getWorkFlowId());
		paramsMap.put("tableId", orders.getId());
		
		Task task = this.taskDao.getTaskByWkFlIdAndTableId(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		TaskDetail last = this.taskDetailDao.getLastTaskDetail(paramsMap);
		
		TaskProvider taskProvider = new PurOrSaleUpdateTaskProviderImpl(orders, task, last);
		
		TaskDetailProvider taskDetailProvider = new PurOrSaleSaveTaskDetailProviderImpl(orders, last);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		paramsMap.put("ownerId", ownerId);
		TaskOwner taskOwner = this.taskOwnerDao.getTaskOwnerByTaskIdAndOwnerId(paramsMap);
		TaskOwnerProvider taskOwnerProvider = new PurOrSaleUpdateTaskOwnerProviderImpl(taskOwner, orders);
		
		return taskEngineService.update(taskProvider, taskDetailProvider, taskOwnerProvider);
		
	}
	
	public boolean updateOrders(PurOrSaleOrder orders) {
		return update(orders, orders.getCreatorCompanyId());
	}
	
	public boolean updateToSubmitOrders(PurOrSaleOrder orders) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("workFlowId", orders.getWorkFlowId());
		paramsMap.put("tableId", orders.getId());
		
		Task task = this.taskDao.getTaskByWkFlIdAndTableId(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		TaskDetail last = this.taskDetailDao.getLastTaskDetail(paramsMap);
		
		TaskProvider taskProvider = new PurOrSaleUpdateTaskProviderImpl(orders, task, last);
		
		TaskDetailProvider taskDetailProvider = new PurOrSaleSaveTaskDetailProviderImpl(orders, last);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		paramsMap.put("ownerId", orders.getCreatorCompanyId());
		TaskOwner taskOwner = this.taskOwnerDao.getTaskOwnerByTaskIdAndOwnerId(paramsMap);
		TaskOwnerProvider taskOwnerProvider = new PurOrSaleUpdateToSubmitTaskOwnerProviderImpl(taskOwner, orders);
		
		return taskEngineService.update(taskProvider, taskDetailProvider, taskOwnerProvider);
	}
	
	public boolean deleteOrders(PurOrSaleOrder orders) {
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("workFlowId", orders.getWorkFlowId());
		paramsMap.put("tableId", orders.getId());
		
		Task task = this.taskDao.getTaskByWkFlIdAndTableId(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		TaskDetail last = this.taskDetailDao.getLastTaskDetail(paramsMap);
		
		TaskProvider taskProvider = new PurOrSaleUpdateTaskProviderImpl(orders, task, last);
		
		TaskDetailProvider taskDetailProvider = new PurOrSaleSaveTaskDetailProviderImpl(orders, last);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		paramsMap.put("ownerId", orders.getCreatorCompanyId());
		TaskOwner taskOwner = this.taskOwnerDao.getTaskOwnerByTaskIdAndOwnerId(paramsMap);
		TaskOwnerProvider taskOwnerProvider = new PurOrSaleDeleteTaskOwnerProviderImpl(taskOwner, orders);
		
		return taskEngineService.update(taskProvider, taskDetailProvider, taskOwnerProvider);
	}
	
	public boolean takeOverOrders(PurOrSaleOrder orders) {
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("workFlowId", orders.getWorkFlowId());
		paramsMap.put("tableId", orders.getId());
		
		Task task = this.taskDao.getTaskByWkFlIdAndTableId(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		TaskDetail last = this.taskDetailDao.getLastTaskDetail(paramsMap);
		
		TaskProvider taskProvider = new PurOrSaleUpdateTaskProviderImpl(orders, task, last);
		
		TaskDetailProvider taskDetailProvider = new PurOrSaleSaveTaskDetailProviderImpl(orders, last);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		paramsMap.put("ownerId", orders.getSellerId());
		TaskOwner taskOwner = this.taskOwnerDao.getTaskOwnerByTaskIdAndOwnerId(paramsMap);
		TaskOwnerProvider taskOwnerProvider = new PurOrSaleTakeOverTaskOwnerProviderImpl(taskOwner, orders);
		
		return taskEngineService.update(taskProvider, taskDetailProvider, taskOwnerProvider);
		
	}
	
	public boolean takeOverUpdateOrders(PurOrSaleOrder orders) {
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("workFlowId", orders.getWorkFlowId());
		paramsMap.put("tableId", orders.getId());
		
		Task task = this.taskDao.getTaskByWkFlIdAndTableId(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		TaskDetail last = this.taskDetailDao.getLastTaskDetail(paramsMap);
		
		TaskProvider taskProvider = new PurOrSaleUpdateTaskProviderImpl(orders, task, last);
		
		TaskDetailProvider taskDetailProvider = new PurOrSaleSaveTaskDetailProviderImpl(orders, last);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		paramsMap.put("ownerId", orders.getSellerId());
		TaskOwner s_taskOwner = this.taskOwnerDao.getTaskOwnerByTaskIdAndOwnerId(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("taskId", task.getId());
		paramsMap.put("ownerId", orders.getBuyerId());
		TaskOwner b_taskOwner = this.taskOwnerDao.getTaskOwnerByTaskIdAndOwnerId(paramsMap);
		
		TaskOwnerProvider taskOwnerProvider = new PurOrSaleTakeOverToUpdateTaskOwnerProviderImpl(b_taskOwner, s_taskOwner, orders);
		
		return taskEngineService.update(taskProvider, taskDetailProvider, taskOwnerProvider);
		
	}
	
	public boolean takeOverUpdateToSubmitOrders(PurOrSaleOrder orders) {
		return takeOverUpdateOrders(orders);
	}
	
	public boolean dealWithOrders(PurOrSaleOrder orders) {
		return update(orders, orders.getCreatorCompanyId());
	}
	
	public boolean agreeToRecieve(PurOrSaleOrder orders) {
		return update(orders, orders.getBuyerId());
	}
	
	public boolean turnBack(PurOrSaleOrder orders) {
		return update(orders, orders.getSellerId());
	}
	
	public boolean recieve(PurOrSaleOrder orders) {
		return update(orders, orders.getBuyerId());
	}
	
	public boolean sale(PurOrSaleOrder orders) {
		return update(orders, orders.getSellerId());
	}
}
